<?php
//by willson 2016/5/10
//简化请求数据的获取
class Request {
	public function __construct() {
		//
	}

	public function request($name, $default='', $type='string') {
		$value = $this->act($name, $default, $type, 'request');
		return $value;
	}

	public function get($name, $default='', $type='string') {
		$value = $this->act($name, $default, $type, 'get');
		return $value;
	}

	public function post($name, $default='', $type='string') {
		$value = $this->act($name, $default, $type, 'post');
		return $value;
	}

	public function cookie($name, $default='', $type='string') {
		$value = $this->act($name, $default, $type, 'cookie');
		return $value;
	}

	public function server($name, $default='', $type='string') {
		$value = $this->act($name, $default, $type, 'server');
		return $value;
	}

	public function session($name, $default='', $type='string') {
		$value = $this->act($name, $default, $type, 'session');
		return $value;
	}
	
	public function file($mydir, $name, $upload_upyun=true) {
		$value = $this->act($name, $upload_upyun, $mydir, 'file');
		return $value;
	}
	
	public function origin($name, $default='') {
		$value = $this->act($name, $default, 'origin', 'post');
		return $value;
	}

	public function act($name, $default='', $type='string', $method='get') {
		$value = '';
		$data = array();
		if (!is_array($method)) {
			switch ($method) {
				case 'get':
					$data = $_GET;
					break;
				case 'post':
					$data = $_POST;
					break;
				case 'request':
					$data = $_REQUEST;
					break;
				case 'cookie':
					$data = $_COOKIE;
					break;
				case 'server':
					$data = $_SERVER;
					break;
				case 'session':
					$data = $_SESSION;
					break;
				case 'file':
					$url = upload_one_file($type, $name, $default);
					$data = array("{$name}"=>$url);
					$default = '';
					break;
			}
		} else {
			$data = $method;
		}
		if ($type == 'string' && (is_int($default) || is_integer($default))) $type = 'int';
		if ($type == 'string' && (is_float($default) || is_double($default))) $type = 'float';
		switch ($type) {
			case 'int':
			case 'integer':
				if ($default == '') $default = 0;
				$value = isset($data[$name]) ? intval($data[$name]) : $default;
				break;
			case 'float':
			case 'double':
			case 'decimal':
				if ($default == '') $default = 0;
				$value = isset($data[$name]) ? floatval($data[$name]) : $default;
				break;
			case 'origin': //原样返回
				$value = isset($data[$name]) ? $data[$name] : $default;
				break;
			case 'urldecode': //url解码
				$value = (isset($data[$name]) && strlen(trim($data[$name]))) ? trim(urldecode($data[$name])) : $default;
				break;
			case 'slash': //去斜杠
			case 'xg':
				$value = (isset($data[$name]) && strlen(trim($data[$name]))) ? stripslashes(trim($data[$name])) : $default;
				break;
			case 'array': //转数组
				$value = isset($data[$name]) ? $data[$name] : $default;
				if (!is_array($value)) $value = $default;
				break;
			case 'json': //转json对象
			case 'object':
			case 'json[]':
				$is_array = false;
				if ($type == 'json[]') $is_array = true;
				$value = (isset($data[$name]) && strlen(trim($data[$name]))) ? stripslashes(trim($data[$name])) : $default;
				$value = $value ? json_decode($value, $is_array) : $default;
				break;
			default:
				$value = (isset($data[$name]) && strlen(trim($data[$name]))) ? strval(trim($data[$name])) : $default;
				break;
		}
		return $value;
	}
}
